package main

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"time"
)

func main() {
	a := app.New()
	w := a.NewWindow("Animation Demo")
	var button *widget.Button
	button = widget.NewButton("Moving Button", func() {
		// 点击按钮时启动动画
		go animateButton(button, w.Canvas())
	})

	w.SetContent(container.NewVBox(button))
	w.Resize(fyne.NewSize(400, 300))
	w.ShowAndRun()
}

func animateButton(button fyne.CanvasObject, canvas fyne.Canvas) {
	startPos := button.Position()
	endPos := fyne.NewPos(startPos.X+200, startPos.Y)
	duration := time.Second * 2 // 动画持续时间

	ticker := time.NewTicker(time.Millisecond * 16) // 每帧大约60FPS
	defer ticker.Stop()

	startTime := time.Now()
	for range ticker.C {
		progress := time.Since(startTime).Seconds() / duration.Seconds()
		if progress >= 1 {
			break
		}
		newX := float32(startPos.X) + float32(float32(endPos.X-startPos.X)*float32(progress))
		button.Move(fyne.NewPos(newX, startPos.Y))
		canvas.Refresh(button)
	}
	button.Move(endPos) // 最后确保将按钮移动到最终位置
	canvas.Refresh(button)
}
